Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Missing {res} when -display- is*used in a loop

    Stata erroneously omits {res} when -display- is used in a loop, causing the displayed result to take the display style of the previous line. If there is no other previous style, it takes the {com} style of the loop itself.

    When -display- is used in -foreach-, -forvalues-, or -while-, it should print:
    Code:
    {res} foo
    But instead it prints:
    Code:
    foo
    This is not a new issue, and as of five years ago technical support even reported that they were "taking a look". I've re-reported the issue, but am also posting here for searchability. In addition, I clarify that the issue is not simply that "as error" persists. Other styles also persist, such as "as text", "as input", and even the "command" style of the loop itself.

    Code:
    // Demonstrate issue with -forvalues-:
    forvalues i = 1/1 {
        display "implicit res <- bug"
        display as err "explicit err <- properly styled"
        display "implicit res <- bug"
        display as txt "explicit txt <- properly styled"
        display "implicit res <- bug"
        display as com "explicit com <- properly styled"
        display "implicit res <- bug"
        display as res "explicit res <- properly styled"
        display "implicit res <- technically bug, but properly styled anyway"
    }
    Code:
    . // Demonstrate issue with -forvalues-:
    . forvalues i = 1/1 {c -(}
    {txt}  2{com}.     display "implicit res <- bug"
    {txt}  3{com}.     display as err "explicit err <- properly styled"
    {txt}  4{com}.     display "implicit res <- bug"
    {txt}  5{com}.     display as txt "explicit txt <- properly styled"
    {txt}  6{com}.     display "implicit res <- bug"
    {txt}  7{com}.     display as com "explicit com <- properly styled"
    {txt}  8{com}.     display "implicit res <- bug"
    {txt}  9{com}.     display as res "explicit res <- properly styled"
    {txt} 10{com}.     display "implicit res <- technically bug, but properly styled anyway"
    {txt} 11{com}. {c )-}
    implicit res <- bug
    {err}explicit err <- properly styled
    implicit res <- bug
    {txt}explicit txt <- properly styled
    implicit res <- bug
    {com}explicit com <- properly styled
    implicit res <- bug
    {res}explicit res <- properly styled
    implicit res <- technically bug, but properly styled anyway
    Click image for larger version

Name:	issue_with_display_in_loop.png
Views:	1
Size:	106.3 KB
ID:	1654293

    Attached Files

  • #2
    Stata Technical Support has responded to me, saying that this behavior is intended, because,

    Sometimes you want to put SMCL into a certain mode/color and have the subsequent -display- commands come out in whatever mode you set. Therefore -display- itself can't reset the SMCL mode.
    This happens in loops as well, because loops are "implicit programs":

    Inside a program or an implicit program (i.e. a loop, which needs to create a hidden program to store the lines inside the loop to be executed over and over), the SMCL mode is not set or reset at any point.
    However a running do-file/ado-file is not an implicit program, so the behavior is different there. This doesn't seem optimal to me, but I will learn to live with it.

    Even Stata's own documentation, such as the examples in -help foreach-, ends up printing text in {com} mode.

    Code:
    foreach name in "Annette Fett" "Ashley Poole" "Marsha Martinez" {
            display length("`name'") " characters long -- `name'"
    }
    Long story short: I should get in the habit of always specifying the mode when using -display-.

    Comment

    Working...
    X